package com.zhentao.handler;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName: CanalDatalistnener
 * @Author: 振涛教育_Q
 * @Date: 2024/12/9 18:12*/


@Component
@CanalEventListener
public class CanalDatalistnener {
/**
     * 自定义监听
     *
     * @param eventType 当前操作的类型 (例如：增加数据)
     * @param rowData   发生变更的一行数据*/


    @ListenPoint(
            // 指定监听的类型
            eventType = {CanalEntry.EventType.DELETE, CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT,},
            // 指定监听的数据库
            schema = {"minan"},
            // 指定监听的表
            table = {"tb_files"},
            // 指定实例的地址
            destination = "minan"
    )
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        System.out.println("自定义操作前的数据:");
        for (CanalEntry.Column column : beforeColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
        System.out.println("自定义操作操作后的数据:");
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            System.out.println(column.getName() + ":" + column.getValue());
        }
    }

}
